﻿/*
* Pztz.cs
*
* 功 能： 智能诊断-偏置调整模块的后台类
* 类 名： Pztz
*
* Ver    变更日期  20131126   负责人  黄永生  变更内容
* ───────────────────────────────────
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web.Security;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using Maticsoft.Common;
using DQYT.HTDZ.DBUtility;
using DQYT.HTDZ.BusinessService;
using System.Xml;
using System.Text;
using System.Drawing;
using DQYT.HTDZ.Common;
using DQYT.HTDZ.BLL;
using DQYT.HTDZ.Model;
namespace DQYT.HTDZ.Web.Views.Analysis.balance
{
    public partial class Balance : System.Web.UI.Page
    {          
        private BDVALUEModel valuemodel;       
        protected void Page_Load(object sender, EventArgs e)
        {          
            if (!IsPostBack)
            {
                string wellid = "晋95-52X";
                string jcrq = "2012/8/10 00:00:00";
                //将日期转换格式
               /* string timeS = jcrq;
                int i = timeS.IndexOf("/", 3, timeS.Length - 3);
                string ts = timeS.Substring(i, 5);
                timeS = timeS.Replace(ts, "");
                timeS = ts.Substring(1, 4) + "/" + timeS;
                jcrq = timeS; */ 
                init(wellid,jcrq);
            }
        }

        #region 初始化界面
        /// <summary>
        /// 初始化界面
        /// </summary>
        protected void init(string wellid,string jcrq)
        {          
            //根据井号和日期获得基础数据表
            AnalysisFunc fun = new AnalysisFunc();
            DataTable dt = fun.getCJSJTb(wellid, jcrq);
            if(dt.Rows.Count!=0)
            {
                DataTable updata = null;//上行数据
                DataTable downdata = null;//下行数据

                //提取上行和下行数据
                // 复制表结构
                updata = dt.Clone();
                downdata = dt.Clone();               
                for (int i = 0; i < dt.Rows.Count; i++)
                {

                    if (i < dt.Rows.Count / 2)
                    {
                        // 添加空行
                        DataRow row = updata.NewRow();
                        row = dt.Rows[i];
                        updata.Rows.Add(row.ItemArray);                        
                       // updata.Rows[i][0] = dt.Rows[i][0];                       
                    }
                    else
                    {
                        // 添加空行
                        DataRow row = downdata.NewRow();
                        row = dt.Rows[i];
                        downdata.Rows.Add(row.ItemArray);
                        //downdata.Rows[j++][0] = dt.Rows[i][0];                       
                    }
                }
               
                #region 计算上冲程数据
                double dl_max_up = (double)updata.Rows[0]["ADL"];//上冲程电流最大值
                double gl_max_up = (double)updata.Rows[0]["HYGGL"];//上冲程功率最大值
                double gl_avg_up = (double)updata.Rows[0]["HYGGL"];//上冲程功率平均值
                int gl_avg_up_count = 1;
                if (gl_avg_up == 9999999)//第一个数据为null时，不参与计算
                {
                    gl_avg_up = 0;
                    gl_avg_up_count = 0;
                }

                double glys_one_up = (double)updata.Rows[0]["HGLYS"];//上冲程功率因数
                double glys_two_up = (double)updata.Rows[0]["HGLYS"];
                double glys_three_up = (double)updata.Rows[0]["HGLYS"];
                int glys_count = 1;
                //平均功率因数为负数或null时，不参与计算
                if (glys_three_up <= 0 || glys_three_up == 9999999)
                {
                    glys_three_up = 0;
                    glys_count = 0;
                }                               
                double djxl_one_up = (double)updata.Rows[0]["DJXL"];//上冲程电机效率
                double djxl_two_up = (double)updata.Rows[0]["DJXL"];
                double djxl_three_up = (double)updata.Rows[0]["DJXL"];
                int djxl_three_up_count = 1;
                //电机效率为空时，不参与计算
                if (djxl_three_up == 9999999)//数值类型的为空时，默认为9999999
                {
                    djxl_three_up = 0;
                    djxl_three_up_count = 0;
                }
                

                double dxdy = (double)updata.Rows[0]["ADY"];//单向电压
                int dxdy_count = 1;
                //单向电压第一个数据为空时，不参与计算
                if (dxdy == 9999999)
                {
                    dxdy = 0;
                    dxdy_count = 0;
                }
                double glys_max_up = (double)updata.Rows[0]["HGLYS"];//上冲程功率因数最大值
                //遍历datatable计算取值
                for (int i = 1; i < updata.Rows.Count; i++)
                {
                    #region  电流最大值平衡度法用到的数据
                    if ((double)updata.Rows[i]["ADL"] != 9999999)
                    {
                        if (dl_max_up == 9999999) //如果第一个数据为null（9999999）
                        {
                            dl_max_up = (double)updata.Rows[i]["ADL"];
                        }
                        else
                        { //对比数据均不为null情况下，进行比较
                            if ((double)updata.Rows[i]["ADL"] >= dl_max_up)
                            {
                                dl_max_up = (double)updata.Rows[i]["ADL"];
                                glys_one_up = (double)updata.Rows[i]["HGLYS"];
                                djxl_one_up = (double)updata.Rows[i]["DJXL"];
                                //如果此处的电机效率为空，电机效率等于功率因数
                                if (djxl_one_up == 9999999)
                                {
                                    djxl_one_up = glys_one_up;
                                }

                            }
                        }
                    }                  
                  
                    //单向电压等于电压平均值
                    if ((double)updata.Rows[i]["ADY"] != 9999999)
                    {
                        //将单向电压不为空的数据求和，个数
                        dxdy += (double)updata.Rows[i]["ADY"];
                        dxdy_count++;
                    }
                    #endregion

                    #region 功率最大值平衡度法用到的数据
                    if ((double)updata.Rows[i]["HYGGL"] != 9999999)
                    {
                        if (gl_max_up == 9999999) //如果第一个数据为null（9999999）
                        {
                            gl_max_up = (double)updata.Rows[i]["HYGGL"];
                        }
                        else  //对比数据均不为null情况下，进行比较
                        {
                            if ((double)updata.Rows[i]["HYGGL"] >= gl_max_up)
                            {
                                gl_max_up = (double)updata.Rows[i]["HYGGL"];
                                glys_two_up = (double)updata.Rows[i]["HGLYS"];
                                djxl_two_up = (double)updata.Rows[i]["DJXL"];
                                //如果此处的电机效率为空，电机效率等于功率因数
                                if (djxl_two_up == 9999999)
                                {
                                    djxl_two_up = glys_two_up;
                                }
                            }
                        }
                    }

                  #endregion

                    #region 功率平均值平衡度法用到的数据
                    //上冲程功率求和
                    if ((double)updata.Rows[i]["HYGGL"] != 9999999)
                    {
                        gl_avg_up += (double)updata.Rows[i]["HYGGL"];
                        gl_avg_up_count++;
                    }
                    //平均功率因数
                    if ((double)updata.Rows[i]["HGLYS"] > 0 && (double)updata.Rows[i]["HGLYS"] != 9999999)
                    {
                        glys_three_up += (double)updata.Rows[i]["HGLYS"];
                        glys_count++;
                    }
                    //电机效率（第三块div）                  
                    if ((double)updata.Rows[i]["DJXL"] != 9999999)
                    {
                        djxl_three_up += (double)updata.Rows[i]["DJXL"];
                        djxl_three_up_count++;
                    }
                    #endregion

                    //计算上冲程功率因数最大值
                    if ((double)updata.Rows[i]["HGLYS"] != 9999999)
                    {
                        if (glys_max_up != 9999999)
                        {
                            if ((double)updata.Rows[i]["HGLYS"] >= glys_max_up)
                            {
                                glys_max_up = (double)updata.Rows[i]["HGLYS"];
                            }
                        }
                        else
                        {
                            glys_max_up = (double)updata.Rows[i]["HGLYS"];
                        }
                    }                            
       
                }
                if (gl_avg_up_count == 0)
                {
                    gl_avg_up = 9999999;
                }
                else
                {
                    gl_avg_up = gl_avg_up / gl_avg_up_count;//上冲程功率平均值               
                }
                
                //页面上冲程数据赋值
                //div1
                txt_dlmax_up.Text = getRound(dl_max_up);
                txt_glys_up.Text = getRoundFour(glys_one_up);
                txt_djxl_one_up.Text = getRoundFour(djxl_one_up);               
                //div2
                txt_glmax_up.Text = getRound(gl_max_up);
                txt_djxl_two_up.Text = getRoundFour(djxl_two_up);
                //div3
                txt_glavg_up.Text = getRound(gl_avg_up);                

                #endregion

                #region 计算下冲程数据
                double dl_max_down = (double)downdata.Rows[0]["ADL"];//下冲程电流最大值
                double gl_max_down = (double)downdata.Rows[0]["HYGGL"];//下冲程功率最大值
                double gl_avg_down = (double)downdata.Rows[0]["HYGGL"];//下冲程功率平均值
                int gl_avg_down_count = 1;
                if (gl_avg_down == 9999999)
                {
                    gl_avg_down = 0;
                    gl_avg_down_count = 0;
                }

                double glys_one_down = (double)downdata.Rows[0]["HGLYS"];//下冲程功率因数
                double glys_two_down = (double)downdata.Rows[0]["HGLYS"];
                double glys_three_down = (double)downdata.Rows[0]["HGLYS"];
                glys_count = glys_count + 1;
                //平均功率因数为负数,null 时，不参与计算
                if (glys_three_down <= 0 || glys_three_down == 9999999)
                {
                    glys_three_down = 0;
                    glys_count = glys_count - 1;
                }

                double djxl_one_down = (double)downdata.Rows[0]["DJXL"];//下冲程电机效率
                double djxl_two_down = (double)downdata.Rows[0]["DJXL"];
                double djxl_three_down = (double)downdata.Rows[0]["DJXL"];
                int djxl_three_down_count = 1;
                //电机效率为空时，不参与计算
                if (djxl_three_down == 9999999)
                {
                    djxl_three_down = 0;
                    djxl_three_down_count = 0;
                }
                            
                double glys_max_down = (double)downdata.Rows[0]["HGLYS"];//下冲程功率因数最大值
                //遍历datatable计算取值
                for (int i = 1; i < downdata.Rows.Count; i++)
                {
                    #region 电流最大值平衡度法用到的数据
                    if ((double)downdata.Rows[i]["ADL"] != 9999999)
                    {
                        if (dl_max_down == 9999999)
                        {
                            dl_max_down = (double)downdata.Rows[i]["ADL"];
                        }
                        else
                        {
                            if ((double)downdata.Rows[i]["ADL"] >= dl_max_down)
                            {
                                dl_max_down = (double)downdata.Rows[i]["ADL"];
                                glys_one_down = (double)downdata.Rows[i]["HGLYS"];
                                djxl_one_down = (double)downdata.Rows[i]["DJXL"];
                                //如果此处的电机效率为空，电机效率等于功率因数
                                if (djxl_one_down == 9999999)
                                {
                                    djxl_one_down = glys_one_down;
                                }

                            }
                        }
                    }
                   
                    //单向电压等于电压平均值,先求和、个数
                    if ((double)downdata.Rows[i]["ADY"] != 9999999)
                    {
                        dxdy += (double)downdata.Rows[i]["ADY"];
                        dxdy_count++;
                    }
                    #endregion

                    #region 功率最大值平衡度法用到的数据
                    if ((double)downdata.Rows[i]["HYGGL"] != 9999999)
                    {
                        if (gl_max_down == 9999999)
                        {
                            gl_max_down = (double)downdata.Rows[i]["HYGGL"];
                        }
                        else
                        {
                            if ((double)downdata.Rows[i]["HYGGL"] >= gl_max_down)
                            {
                                gl_max_down = (double)downdata.Rows[i]["HYGGL"];
                                glys_two_down = (double)downdata.Rows[i]["HGLYS"];
                                djxl_two_down = (double)downdata.Rows[i]["DJXL"];
                                //如果此处的电机效率为空，电机效率等于功率因数
                                if (djxl_two_down == 9999999)
                                {
                                    djxl_two_down = glys_two_down;
                                }
                            }
                        }
                    }
                   
                    #endregion

                    #region 功率平均值平衡度法用到的数据
                    //下冲程功率求和
                    if ((double)downdata.Rows[i]["HYGGL"] != 9999999)
                    {
                        gl_avg_down += (double)downdata.Rows[i]["HYGGL"];
                        gl_avg_down_count++;
                    }
                    //平均功率因数
                    if ((double)downdata.Rows[i]["HGLYS"] > 0 && (double)downdata.Rows[i]["HGLYS"] != 9999999)
                    {
                        glys_three_down += (double)downdata.Rows[i]["HGLYS"];
                        glys_count++;
                    }
                    //电机效率（第三块div）                  
                    if ((double)downdata.Rows[i]["DJXL"] != 9999999)
                    {
                        djxl_three_down += (double)downdata.Rows[i]["DJXL"];
                        djxl_three_down_count++;
                    }
                    #endregion

                    //计算下冲程功率因数最大值
                    if ((double)downdata.Rows[i]["HGLYS"] != 9999999)
                    {
                        if (glys_max_down == 9999999)
                        {
                            glys_max_down = (double)downdata.Rows[i]["HGLYS"];
                        }
                        else
                        {
                            if ((double)downdata.Rows[i]["HGLYS"] >= glys_max_down)
                            {
                                glys_max_down = (double)downdata.Rows[i]["HGLYS"];
                            }
                        }
                    }                   
                }
                if (dxdy_count == 0)
                {
                    dxdy = 9999999;
                }
                else
                {
                    dxdy = dxdy / dxdy_count;//电压平均值
                }
                if (gl_avg_down_count == 0)
                {
                    gl_avg_down = 9999999;
                }
                else
                {
                    gl_avg_down = gl_avg_down / gl_avg_down_count;//下冲程功率平均值               
                }

                //页面下冲程数据赋值
                //div1
                txt_dlmax_down.Text = getRound(dl_max_down);
                txt_glys_down.Text = getRoundFour(glys_one_down);
                txt_djxl_one_down.Text = getRoundFour(djxl_one_down);
                txt_dxdy.Text = getRound(dxdy);//单向电压
                //div2
                txt_glmax_down.Text = getRound(gl_max_down);
                txt_djxl_two_down.Text = getRoundFour(djxl_two_down);
                //div3
                txt_glavg_down.Text = getRound(gl_avg_down);               
                #endregion

                double cdxl = 9999999;
                double glys_three = 9999999;
                double cdxl_three = 9999999;
                //将上冲程功率因数最大值和下冲程功率因数最大值比较               
                if (glys_max_up == 9999999)
                {
                    if (glys_max_down != 9999999)
                    {
                        cdxl = glys_max_down * 0.92 * 0.95;
                    }                   
                }               
                else
                {
                    if (glys_max_down == 9999999)
                    {
                        cdxl = glys_max_up * 0.92 * 0.95;
                    }
                    else//二者均不为null
                    {
                        if (glys_max_up > glys_max_down)
                        {
                            //得出功率因数最大值并进行传动效率计算
                            cdxl = glys_max_up * 0.92 * 0.95;
                        }
                        else
                        {
                            cdxl = glys_max_down * 0.92 * 0.95;
                        }
                    }
                }
               
                //计算正的功率因数均值，div3
                if (glys_count != 0)
                {
                    if (glys_three_up == 9999999)
                    {
                        if (glys_three_down != 9999999)
                        {
                            glys_three = glys_three_down / glys_count;
                            //传动效率
                            cdxl_three = glys_three * 0.92 * 0.95;
                        }
                    }
                    else
                    {
                        if (glys_three_down == 9999999)
                        {
                            glys_three = glys_three_up / glys_count;
                            //传动效率
                            cdxl_three = glys_three * 0.92 * 0.95;
                        }
                        else//二者都不为null时，进行汇总计算
                        {
                            glys_three = (glys_three_up + glys_three_down) / glys_count;//正的功率因数求均值 
                            //传动效率
                            cdxl_three = glys_three * 0.92 * 0.95;
                        }
                    }
                   
                }
               
                //第三块div中的电机效率计算
                if (djxl_three_up_count != 0)
                {
                    djxl_three_up = djxl_three_up / djxl_three_up_count;//上冲程电机效率求均值

                }
                if (djxl_three_up == 0)
                {
                    djxl_three_up = glys_three;//电机效率为0时，默认等于功率因数
                }
                if (djxl_three_down_count != 0)
                {
                    djxl_three_down = djxl_three_down / djxl_three_down_count;//下冲程电机效率求均值

                }
                if (djxl_three_down == 0)
                {
                    djxl_three_down = glys_three;
                }

                //div3电机效率赋值
                txt_djxl_three_up.Text = getRoundFour(djxl_three_up);
                txt_djxl_three_down.Text = getRoundFour(djxl_three_down);
                //传动效率赋值
                txt_cdxl_one.Text = getRoundFour(cdxl);
                txt_cdxl_two.Text = getRoundFour(cdxl);
                txt_cdxl_three.Text = getRoundFour(cdxl_three);
               
                //电流最大值平衡度
                if(dl_max_up!=0)
                {
                    if (dl_max_down != 9999999 && dl_max_up != 9999999)
                    {
                        txt_dlmax.Text = getRound((dl_max_down / dl_max_up));
                    }
                }
                //有功最大值平衡度
                if (gl_max_up != 0)
                {
                    if (gl_max_down != 9999999 && gl_max_up != 9999999)
                    {
                        txt_ygmax.Text = getRound((gl_max_down / gl_max_up));
                    }
                }
                //有功平均值平衡度
                if (gl_avg_up != 0)
                {
                    if (gl_avg_down != 9999999 && gl_avg_up != 9999999)
                    {
                        txt_ygavg.Text = getRound((gl_avg_down / gl_avg_up));
                    }
                }
            }

            //根据井号和日期获得特征值           
            BDValueFunc valueFun = new BDValueFunc();
            valuemodel = valueFun.getValueModel(wellid, jcrq);
            string djsczs = getRound(Convert.ToDouble(valuemodel.DDJEDZS));
            //电机输出转速赋值
            txt_djsczs_one.Text = djsczs;
            txt_djsczs_two.Text = djsczs;
            txt_djsczs_three.Text = djsczs;
            string cyjcc = getRound(Convert.ToDouble(valuemodel.CYJCCI));
            //抽油机冲次赋值
            txt_cyjcc_one.Text = cyjcc;
            txt_cyjcc_two.Text = cyjcc;
            txt_cyjcc_three.Text = cyjcc;
            //平衡块重量
            decimal? phkzl;
            //判断平衡块重量是否为null，为空的数据归零处理，最后求和
            phkzl = IsNull(valuemodel.PHKZL1) + IsNull(valuemodel.PHKZL2) + IsNull(valuemodel.PHKZL3) + IsNull(valuemodel.PHKZL4);
            txt_PHKZL.Text =getRound(Convert.ToDouble(phkzl));

            this.txt_WELLID.Text = valuemodel.WELLID.ToString();
            

        }
        #endregion

        #region 通过计算平衡度法，给出建议
        /// <summary>
        /// 通过计算给予相应的建议
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_calculate_Click(object sender, EventArgs e)
        {
            string rblStr = rbl_balance.SelectedValue.ToString();
            double m = Convert.ToDouble(txt_PHKZL.Text);//平衡块重量
            if (m == 0)
            {
                ScriptManager.RegisterStartupScript(this.UpdatePanel1, Page.GetType(), "tishi", "alert('平衡块质量不能为0！');", true);
                return;
            }
            double p_up;
            double p_down;                    
            double m_up;
            double m_down;
            double d_avg;
            switch(rblStr)
            {
                case "dl_max":
                    //电流最大值法计算后，给出建议                   
                    //上冲程功率
                    double zs_one = Convert.ToDouble(txt_djsczs_one.Text);
                    if(zs_one ==0)
                    {
                        ScriptManager.RegisterStartupScript(this.UpdatePanel1, Page.GetType(), "tishi", "alert('电机输出转速不能为0！');", true);
                        return;
                    }
                    p_up = getGL(Convert.ToDouble(txt_dlmax_up.Text),Convert.ToDouble(txt_dxdy.Text),Convert.ToDouble(txt_glys_up.Text));
                    m_up = getM(p_up,Convert.ToDouble(txt_djxl_one_up.Text),zs_one,Convert.ToDouble(txt_cdxl_one.Text),Convert.ToDouble(txt_cyjcc_one.Text));
                   //下冲程数据计算
                    p_down = getGL(Convert.ToDouble(txt_dlmax_down.Text),Convert.ToDouble(txt_dxdy.Text),Convert.ToDouble(txt_glys_down.Text));
                    m_down = getM(p_down,Convert.ToDouble(txt_djxl_one_down.Text),zs_one,Convert.ToDouble(txt_cdxl_one.Text),Convert.ToDouble(txt_cyjcc_one.Text));
                  
                    //计算d均值
                    d_avg = getDAVG(m_up,m_down,m);
                    //后台计算电流最大值平衡度
                    double dl_max =Convert.ToDouble(txt_dlmax_down.Text) / Convert.ToDouble(txt_dlmax_up.Text);
                    dl_max = Convert.ToDouble(getRound(dl_max));
                    //根据d的值判定给出建议
                    txt_advise.Text =getAdvise(d_avg,dl_max);
                    break;
                case "yg_max":
                    //功率最大值法计算后，给出建议   
                    double zs_two = Convert.ToDouble(txt_djsczs_two.Text);
                    if (zs_two == 0)
                    {
                        ScriptManager.RegisterStartupScript(this.UpdatePanel1, Page.GetType(), "tishi", "alert('电机输出转速不能为0！');", true);
                        return;
                    }
                    m_up = getM(Convert.ToDouble(txt_glmax_up.Text), Convert.ToDouble(txt_djxl_two_up.Text), zs_two, Convert.ToDouble(txt_cdxl_two.Text), Convert.ToDouble(txt_cyjcc_two.Text));
                    m_down = getM(Convert.ToDouble(txt_glmax_down.Text), Convert.ToDouble(txt_djxl_two_down.Text), zs_two, Convert.ToDouble(txt_cdxl_two.Text), Convert.ToDouble(txt_cyjcc_two.Text));
                    d_avg = getDAVG(m_up, m_down, m);
                    double gl_max = Convert.ToDouble(txt_glmax_down.Text) / Convert.ToDouble(txt_glmax_up.Text);
                    gl_max = Convert.ToDouble(getRound(gl_max));
                    //给出建议
                    txt_advise.Text = getAdvise(d_avg, gl_max);
                    break;
                case "yg_avg":
                    //功率平均值法计算后，给出建议   
                    double zs_three = Convert.ToDouble(txt_djsczs_three.Text);
                    if (zs_three == 0)
                    {
                        ScriptManager.RegisterStartupScript(this.UpdatePanel1, Page.GetType(), "tishi", "alert('电机输出转速不能为0！');", true);
                        return;
                    }
                    m_up = getM(Convert.ToDouble(txt_glavg_up.Text), Convert.ToDouble(txt_djxl_three_up.Text), zs_three, Convert.ToDouble(txt_cdxl_three.Text), Convert.ToDouble(txt_cyjcc_three.Text));
                    m_down = getM(Convert.ToDouble(txt_glavg_down.Text), Convert.ToDouble(txt_djxl_three_down.Text), zs_three, Convert.ToDouble(txt_cdxl_three.Text), Convert.ToDouble(txt_cyjcc_three.Text));
                    d_avg = getDAVG(m_up, m_down, m);
                    double gl_avg = Convert.ToDouble(txt_glavg_down.Text) / Convert.ToDouble(txt_glavg_up.Text);
                    gl_avg = Convert.ToDouble(getRound(gl_avg));
                    txt_advise.Text = getAdvise(d_avg, gl_avg);
                    break;
                default:
                 //   Response.Write("<script>alert('请选择一项数据进行计算')</script>");
                    break;
            }

        }
        #endregion

        #region 平衡调整方法用到的计算方法
        /// <summary>
        /// 四舍五入保留小数点后两位
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        protected string getRound(double data)
        {
            string str = "";
            if (data != 9999999)//数据不为空时，进行四舍五入保留两位小数
            {
                data = Math.Round((data + 0.001) * Math.Pow(10, 2)) / Math.Pow(10, 2);
                str = Convert.ToString(data);
            }
            return str;
 
        }

        /// <summary>
        /// 四舍五入保留小数点后四位
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        protected string getRoundFour(double data)
        {
            string str = "";
            if (data != 9999999)//数据不为空时，进行四舍五入保留四位小数
            {
                data = Math.Round((data + 0.00001) * Math.Pow(10, 4)) / Math.Pow(10, 4);
                str = Convert.ToString(data);
            }
            return str;

        }

        /// <summary>
        /// 判断数据是否为null
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        protected decimal? IsNull(decimal? data)
        {
            if (data == 9999999)//数据为空时，归零
            {
                data = 0;
            }
            return data;
        }
      
        /// <summary>
        /// 计算功率
        /// </summary>
        /// <param name="dl"></param>
        /// <param name="dy"></param>
        /// <param name="glys"></param>
        /// <returns></returns>
        protected double getGL(double dl,double dy,double glys)
        {
            double gl;
            gl = (3 * dy * dl * glys)/1000;
            return gl;
        }

        /// <summary>
        /// 计算减速箱输出扭矩
        /// </summary>
        /// <param name="gl"></param>
        /// <param name="djxl"></param>
        /// <param name="zs"></param>
        /// <param name="cdxl"></param>
        /// <param name="cyjcc"></param>
        /// <returns></returns>
        protected double getM(double gl, double djxl, double zs, double cdxl, double cyjcc)
        {
            double m = 0;
            double t;
            if (zs != 0)
            {
                t = 9549 * (djxl * gl / zs);
                m = cdxl * t * (cyjcc / zs);
            }
            return m;
        }

        /// <summary>
        /// 计算最后的d均值
        /// </summary>
        /// <param name="m_up"></param>
        /// <param name="m_down"></param>
        /// <param name="m"></param>
        /// <returns></returns>
        protected double getDAVG(double m_up,double m_down,double m)
        {
            double d_avg=0;
            double m_avg;
            if (m != 0)
            {
                m_avg = (m_down - m_up) / 2;
                //根据油田不同，g可能会更改
                d_avg = m_avg / (m * 9.8);
            }          
            return d_avg;
        }
        
        #endregion

        #region 根据数据给出建议
        /// <summary>
        /// 根据d均值和平衡度给出相应建议
        /// </summary>
        /// <param name="d_avg"></param>
        /// <param name="phd"></param>
        /// <returns></returns>
        protected string getAdvise(double d_avg,double phd)
        {
            string advise = "";
           
                double d = Math.Abs(d_avg) / 2;
                d = d * 100;
                d = Math.Round((d + 0.01) * Math.Pow(10, 1)) / Math.Pow(10, 1);//保留一位小数
                int n = Convert.ToInt32(Math.Floor(d / 2.5));//对2.5个单位取整
                int m = n + 1;
                if (d_avg > 0)//向外移
                {
                    if (phd > 1)
                    {
                        advise = "建议将两块平衡块同时向外移" + d.ToString() + "厘米";
                    }
                    else if (phd < 0.85)
                    {
                        advise = "建议将两块平衡块同时向外移" + d.ToString() + "厘米";
                    }
                    else
                    {
                        advise = "建议将两块平衡块同时向外移" + d.ToString() + "厘米";
                    }

                }
                else 
                {//向内移
                    if (phd > 1)
                    {
                        advise = "建议将两块平衡块同时向内移" + d.ToString() + "厘米";
                    }
                    else if (phd < 0.85)
                    {
                        advise = "建议将两块平衡块同时向内移" + d.ToString() + "厘米";
                    }
                    else
                    {
                        advise = "建议将两块平衡块同时向外移" + d.ToString() + "厘米";
                    }
                    
                }

          
            return advise;
        }
        #endregion



    }
}
